package com.orange.core.service.category;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.core.mapper.category.ProductCategoryMapper;
import com.orange.core.mapper.product.CreditProductMapper;
import com.orange.core.pojo.entity.category.ProductCategory;
import com.orange.core.pojo.entity.product.CreditProduct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, ProductCategory> {

    private final CreditProductMapper creditProductMapper;

    public boolean del(int id) {
        Long productCount = creditProductMapper.selectCount(new LambdaQueryWrapper<CreditProduct>()
                .eq(CreditProduct::getCategoryId, id));
        Assert.equals(productCount, 0, "商品分类下还有积分商品，无法删除");

        return this.removeById(id);
    }
}
